package org.algorithm.YaYa.链表;



import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * good luck guys
 * keep learing for algorithm
 */
public class TwoSum {
    public static void main(String[] args) {
        int[] nums = new int[]{3,2,4};
        int target = 6;
//        System.out.println(Arrays.toString(calculateSumNumbers(nums, target)));
        System.out.println(Arrays.toString(towSum(nums, target)));
    }

    /**
     * 方法一
     * @param array
     * @param target
     * @return
     */
    public static int[] calculateSumNumbers(int[] array, int target) {
        int[] resultArray = new int[2];
         for (int i = 0; i < array.length; i++) {
            int tempNum = array[i];
            for (int j = i+1; j < array.length; j++) {
                if (array[j] == target - tempNum) {
                    resultArray[0] = i;
                    resultArray[1] = j;
                    return resultArray;
                }
            }
        }
      return null;
    }
    /**
     * 方法二
     */
    public static int[] towSum(int[] array, int target){
        int[] result = new int[2];
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        for (int i = 0; i < array.length; i++) {
            if(map.containsKey(array[i])){
                result[0] = i;
                result[1] = map.get(array[i]);
                return result;
            }
            map.put(target-array[i],i);

        }
        return result;
    }

}
